package licode.unisop.realm.domainx.service;

import licode.unisop.provider.api.SiAuthenticationProvider;
import licode.unisop.provider.info.SiIdentityContext;
import licode.unisop.provider.info.SiReturnCode;
import licode.unisop.provider.vo.SiIdentity;
import licode.unisop.types.dp.AuthType;
import org.springframework.stereotype.Service;

/**
 * 默认提供的用户认证接口实现
 *
 * @author licode
 */
@Service
public class SiAuthenticationProviderDefault implements SiAuthenticationProvider {
    @Override
    public String getName() {
        return "默认";
    }

    @Override
    public SiReturnCode authenticate(SiIdentityContext context) {
        SiIdentity exist = context.getExist();
        SiIdentity target = context.getTarget();

        // 微信方式登录，直接返回成功
        if (AuthType.WX_AUTH.getType().equals(target.getAuthType())) {
            return SiReturnCode.builder().code(0).message("校验成功").build();
        } else if (AuthType.PASSWORD.getType().equals(target.getAuthType())) {
            if (exist.getAuthCode().equals(target.getAuthCode())) {
                return SiReturnCode.builder().code(0).message("校验成功").build();
            } else {
                return SiReturnCode.builder().code(-1).message("用户名或密码错误").build();
            }
        } else {
            return SiReturnCode.builder().code(-1).message("不支持认证方式").build();
        }
    }
}
